package com.wfk.h5.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wfk.entity.Account;
import com.wfk.entity.BankDetails;
import com.wfk.entity.BankType;
import com.wfk.entity.Customer;
import com.wfk.entity.ReceivedAddr;
import com.wfk.entity.SysCashRule;
import com.wfk.service.IAccountService;
import com.wfk.service.IAddressService;
import com.wfk.service.IBankDetailsService;
import com.wfk.service.IBankTypeService;
import com.wfk.service.IBasicsSmsInfoService;
import com.wfk.service.ICustomerService;
import com.wfk.service.IReceivedAddrService;
import com.wfk.service.ISysCashDetailsService;
import com.wfk.service.ISysCashRuleService;
import com.wfk.utils.MD5Util;
import com.wfk.utils.ResponseData;
import com.wfk.utils.ValidateUtil;

@Service
public class MemberService {
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IBasicsSmsInfoService basicsSmsInfoService;
	@Autowired
	private IBankDetailsService bankDetailsService;
	@Autowired
	private IBankTypeService bankTypeService;
	@Autowired
	private IReceivedAddrService receivedAddrService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private ISysCashRuleService sysCashRuleService;
	@Autowired
	private ISysCashDetailsService sysCashDetailsService;
	@Autowired
	private IAccountService accountService;
	
	public ResponseData cashRule(Integer cid) {
		ResponseData webResult = ResponseData.ok();
		SysCashRule rule = sysCashRuleService.getOne(null);
		List<BankDetails> bank = this.bankDetailsService.myBankDetail(cid);
		Account account = this.accountService.get(cid);
		webResult.putDataValue("result", bank);
		webResult.putDataValue("rule", rule);
		webResult.putDataValue("account", account);
		return webResult;

	}
	
	public ResponseData saveReceivedAddr(Integer id,Integer cid,String name,String phone,Integer proid,Integer cityid,Integer areaid
			,String address,Integer isdefault){
			Customer cus = customerService.getById(cid);
			if(ValidateUtil.objEmpty(cus)){
				return ResponseData.baseServiceError("用户未注册");
			}else if(ValidateUtil.objEmpty(proid)
					||ValidateUtil.objEmpty(cityid)
					||ValidateUtil.objEmpty(areaid)){
				return ResponseData.baseServiceError("请选择地址");
			}
			ReceivedAddr vo = new ReceivedAddr();
			String str =  this.addressService.getPingName(proid, cityid, areaid);
			vo.setId(id);
			vo.setCid(cid);
			vo.setName(name);
			vo.setPhone(phone);
			vo.setProid(proid);
			vo.setCityid(cityid);
			vo.setAreaid(areaid);
			vo.setCid(cid);
			vo.setLocation(str);
			vo.setAddress(address);
			vo.setIsdefault(isdefault);
			if(ValidateUtil.isEquals(isdefault,0)){
				this.receivedAddrService.upDefault(cid);
			}
			boolean saveFalg = this.receivedAddrService.saveOrUpdate(vo);
			if(!saveFalg){
				return ResponseData.baseServiceError("保存失败");
			}
		return ResponseData.ok();
	}
	
	public ResponseData upDefault(Integer cid,Integer id){
		ReceivedAddr addr = this.receivedAddrService.getById(id);
		if(addr==null){
			return ResponseData.baseServiceError("未找到地址");
		}else if(!ValidateUtil.isEquals(cid, addr.getCid())){
			return ResponseData.baseServiceError("非法操作");
		}
		this.receivedAddrService.upDefault(cid);
		addr.setIsdefault(0);
		this.receivedAddrService.updateById(addr);
		return ResponseData.ok();
	}
	/**
	 * 忘记密码
	 * @param cid
	 * @param phone
	 * @param password
	 * @param validcode
	 * @return
	 */
	public ResponseData updatePassword(String phone,String password,String validcode){
		QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>();
		queryWrapper.eq("phone", phone);
		Customer cus = customerService.getOne(queryWrapper);
		if(cus==null){
			return ResponseData.baseServiceError("用户未注册");
		}else if(!cus.getPhone().equals(phone)){
			return ResponseData.baseServiceError("不能修改其他人操作");
		}else if(!ValidateUtil.empty(password)){
			return ResponseData.baseServiceError("密码8~16位字母或数字组合");
		}
		String pwd = MD5Util.md5Hex(password);
		if(pwd.equals(cus.getPassword())){
			return ResponseData.baseServiceError("新密码不能与旧密码相同");
		}
//		ResponseData result=basicsSmsInfoService.validSMS(cus.getPhone(),validcode);
//		if(result.getCode()!=200) {
//			return ResponseData.baseServiceError(result.getMessage());
//		}
		cus.setPassword(pwd);
		boolean updatefalg = this.customerService.updateById(cus);
		if(updatefalg){
			return ResponseData.ok("密码修改成功，请重新登录");
		}
		return ResponseData.baseServiceError("密码修改失败，请重新登录");
	}
	
	/**
	 * 忘记密码
	 * @param cid
	 * @param phone
	 * @param password
	 * @param validcode
	 * @return
	 */
	public ResponseData updatePayword(Integer cid,String paywode,String validcode){
		Customer cus = this.customerService.getById(cid);
		if(cus==null){
			return ResponseData.baseServiceError("用户未注册");
		}else if(!ValidateUtil.empty(paywode)){
			return ResponseData.baseServiceError("支付密码1~6数字");
		}
		String pwd = MD5Util.md5Hex(paywode);
//		ResponseData result=basicsSmsInfoService.validSMS(cus.getPhone(),validcode);
//		if(result.getCode()!=200) {
//			return ResponseData.baseServiceError(result.getMessage());
//		}
		cus.setPayword(pwd);
		boolean updatefalg = this.customerService.updateById(cus);
		if(updatefalg){
			return ResponseData.ok("支付密码修改成功");
		}
		return ResponseData.baseServiceError("密码修改失败，请重新登录");
	}
	

	public ResponseData updateName(Integer cid,String headurl,String nickname){
		Customer cus = this.customerService.getById(cid);
		if(cus==null){
			return ResponseData.baseServiceError("会员为注册");
		}else if(!ValidateUtil.empty(nickname)){
			return ResponseData.baseServiceError("请输入名称");
		}
		cus.setRealname(nickname);
		if(ValidateUtil.empty(headurl)) {
			cus.setHeadurl(headurl);
		}
		this.customerService.updateById(cus);
		return ResponseData.ok();
	}
	
	/**
	 * 绑定银行卡号 
	 * @param cid
	 * @param realname
	 * @param idcard
	 * @param bankcode
	 * @param typeid
	 * @param phone
	 */
	public ResponseData saveBank(Integer cid,String realname,String idcard,String bankcode,Integer typeid,String phone){
			Customer cus = this.customerService.getById(cid);
			if(ValidateUtil.objEmpty(cus)||ValidateUtil.isEquals(cus.getStatus(), -1)){
				return ResponseData.baseServiceError("用户未注册或已冻结");
			}
			BankType bantype = bankTypeService.getById(typeid);
			if(ValidateUtil.objEmpty(bantype)||ValidateUtil.isEquals(bantype.getStatus(), -1)){
				return ResponseData.baseServiceError("开户行不支持");
			}
			QueryWrapper<BankDetails> queryWrapper = new QueryWrapper<BankDetails>();
			queryWrapper.eq("bankcode",bankcode);
			queryWrapper.eq("targetid", cid);
			Integer count = this.bankDetailsService.count(queryWrapper);
			if(count!=null&&count>0){
				return ResponseData.baseServiceError("银行卡已存在");
			}
			BankDetails vo = new BankDetails();
			vo.setTargetid(cid);
			vo.setRealname(realname);
			vo.setIdcard(idcard);
			vo.setBankcode(bankcode);
			vo.setTypeid(typeid);
			vo.setPhone(phone);
			vo.setBankname(bantype.getBankname());
			vo.setLogo(bantype.getLogo());
			boolean savefalg = this.bankDetailsService.save(vo);
			if(savefalg){
				return ResponseData.ok("绑定成功");
			}
		return ResponseData.baseServiceError("系统繁忙，绑定失败");
	}
	
	/**
	 * 是否设置支付
	 * @param cid
	 * @return 如果设置了支付密码返回true 否则返回false
	 */
	public boolean isPayword(Integer cid){
		Customer cus = this.customerService.getById(cid);
		if(!ValidateUtil.objEmpty(cus)) {
			return ValidateUtil.empty(cus.getPayword());
		}
		return false;
	}
	
	/**
	 * 是否绑定银行卡
	 * @param cid
	 * @return 已有绑定银行卡返回true不是返回false
	 */
	public boolean isBankDetail(Integer cid){
		QueryWrapper<BankDetails> queryWrapper = new QueryWrapper<BankDetails>();
		queryWrapper.eq("targetid", cid);
		return bankDetailsService.count(queryWrapper)>0;
	}
}
